<template>
  <div class="taskplan">
    <el-card class="hover">
      <!-- 任务列表 -->
      <div class="show-plan">
        <!-- 列表操作 -->
        <div class="handle-task">
          <div class="add-task">
            <el-button type="primary" size="small" @click="addTask" icon="el-icon-plus">新建任务</el-button>
          </div>
          <div class="add-task">
            <el-button type="primary" size="small" @click="tempTask" icon="el-icon-time">临时任务</el-button>
          </div>
          <div class="delete-tasks">
            <el-button type="danger" size="small" @click="deleteTasks" icon="el-icon-minus">批量删除</el-button>
          </div>
        </div>
        <!-- 列表展示 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="30" align="center"></el-table-column>
          <el-table-column prop="count" label="序号" width="50" align="center"></el-table-column>
          <el-table-column prop="planNumber" label="计划编号" width="120" align="center"></el-table-column>
          <el-table-column prop="taskNumber" label="任务编号" width="120" align="center"></el-table-column>
          <el-table-column prop="typeName" label="任务类型" width="120" align="center"></el-table-column>
          <el-table-column prop="startTime" label="开始时间" width="120" align="center"></el-table-column>
          <el-table-column prop="endTime" label="结束时间" width="120" align="center"></el-table-column>
          <el-table-column prop="patrolGrid" label="巡查范围" width="120" align="center"></el-table-column>
          <el-table-column prop="roleName" label="负责人" width="120" align="center"></el-table-column>
          <el-table-column prop="taskStatus" label="任务状态" width="120" align="center"></el-table-column>
          <el-table-column prop="taskProgress" label="任务进度" width="120" align="center"></el-table-column>
          <el-table-column prop="checkingNumber" label="完成率" width="120" align="center"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="200" align="center"></el-table-column>
          <el-table-column fixed="right" width="300" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="info" @click="openDetailWin(scope.row)" size="mini">详情</el-button>
              <el-button
                type="warning"
                @click="openEditWin(scope.row)"
                size="mini"
                v-if="scope.row.taskStatus == '未处置'"
                >编辑</el-button
              >
              <el-button
                type="danger"
                @click="deleteTask(scope.row)"
                size="mini"
                v-if="scope.row.taskStatus == '未处置'"
                >删除</el-button
              >
              <el-button
                type="success"
                @click="openDistributionWin(scope.row)"
                size="mini"
                v-if="scope.row.taskStatus == '未处置'"
                >处置</el-button
              >
              <el-button
                type="success"
                @click="openProgressWin(scope.row)"
                size="mini"
                v-if="scope.row.taskStatus == '已处置'"
                >事项进度</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 任务处置 -->
        <el-dialog title="计划处置" :visible.sync="distributionWin" width="30%">
          <el-row>
            <el-col :span="20">
              <div class="grid-content bg-purple">
                <el-form label-position="right" label-width="150px" :model="distributionData">
                  <el-form-item label="任务编号：">
                    <el-input v-model="distributionData.taskNumber" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="任务类型：">
                    <el-input v-model="distributionData.typeName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="处置岗位：">
                    <el-select v-model="distributionParams.roleId" clearable placeholder="请选择处置岗位">
                      <el-option v-for="item in roleList" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
          <div slot="footer" class="dialog-footer">
            <el-button @click="distributionWin = false">取 消</el-button>
            <el-button type="primary" @click="distributionTask">处 置</el-button>
          </div>
        </el-dialog>
        <!-- 详情 -->
        <el-dialog title="任务详情" :visible.sync="taskDetailWin" width="30%">
          <el-row>
            <el-col :span="20">
              <div class="grid-content bg-purple">
                <el-form label-position="right" label-width="120px" :model="taskDetailData">
                  <el-form-item label="计划编号：" v-if="taskDetailData.planNumber">
                    <el-input v-model="taskDetailData.planNumber" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="所属计划：" v-if="taskDetailData.planName">
                    <el-input v-model="taskDetailData.planName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="任务编号：">
                    <el-input v-model="taskDetailData.taskNumber" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="任务类型：">
                    <el-input v-model="taskDetailData.typeName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="开始时间：">
                    <el-input v-model="taskDetailData.startTime" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="结束时间：">
                    <el-input v-model="taskDetailData.endTime" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="巡查范围：">
                    <el-input v-model="taskDetailData.patrolGrid" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="巡查主体：">
                    <el-input v-model="taskDetailData.patrolSubject" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="主体类型：">
                    <el-input v-model="taskDetailData.subjectType" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="任务描述：">
                    <el-input type="textarea" :rows="3" :disabled="true" v-model="taskDetailData.taskDescribe">
                    </el-input>
                  </el-form-item>
                  <el-form-item label="巡查事项：" v-show="taskDetailData.taskStatus == '未处置'">
                    <el-button type="success" size="small" @click="checkFile(taskDetailData.content)"
                      >查看文件</el-button
                    >
                  </el-form-item>
                  <el-form-item label="任务进度：" v-show="taskDetailData.taskStatus == '已处置'">
                    <el-input v-model="taskDetailData.taskProgress" :disabled="true"> </el-input>
                  </el-form-item>
                  <el-form-item label="完成率：" v-show="taskDetailData.taskStatus == '已处置'">
                    <el-input v-model="taskDetailData.checkingNumber" :disabled="true"> </el-input>
                  </el-form-item>
                  <el-form-item label="检查企业数量：" v-show="taskDetailData.taskStatus == '已处置'">
                    <el-input v-model="taskDetailData.checkingNumber" :disabled="true"> </el-input>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
        </el-dialog>
        <!-- 抽屉-巡查事项文件 -->
        <el-drawer title="巡查事项" :visible.sync="patrolFileWin" direction="rtl" size="70%">
          <table border="1" class="patrol-file">
            <thead>
              <tr>
                <th v-for="(item, index) in tableHeader" :key="index">{{ item }}</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in tableBody" :key="index">
                <template v-for="(cell, index2) in item">
                  <td :key="index2">{{ cell }}</td>
                </template>
              </tr>
            </tbody>
          </table>
        </el-drawer>
        <!-- 新建任务 -->
        <el-dialog :title="changeStatus ? '编辑任务' : '新建任务'" :visible.sync="createTaskWin" width="40%">
          <el-row>
            <el-col :span="20">
              <div class="grid-content bg-purple">
                <el-form label-position="right" label-width="120px" :model="createTaskData">
                  <el-form-item label="计划编号：">
                    <el-input v-model="createTaskData.planNumber" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="所属计划：">
                    <el-select
                      v-model="createTaskParams.specialPlanId"
                      placeholder="请选择所属计划："
                      style="width: 100%"
                    >
                      <el-option v-for="item in planList" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="巡查主体：">
                    <el-input v-model="createTaskData.patrolSubject" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="主体类型：">
                    <el-input v-model="createTaskData.subjectType" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="任务类型：">
                    <el-input v-model="createTaskData.typeName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="巡查范围：">
                    <el-input v-model="createTaskData.patrolGrid" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="开始时间：">
                    <div class="block">
                      <el-date-picker
                        v-model="createTaskParams.startTime"
                        type="date"
                        placeholder="选择开始日期："
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </div>
                  </el-form-item>
                  <el-form-item label="结束时间：">
                    <div class="block">
                      <el-date-picker
                        v-model="createTaskParams.endTime"
                        type="date"
                        placeholder="选择结束日期："
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </div>
                  </el-form-item>
                  <el-form-item label="任务描述：">
                    <el-input type="textarea" :rows="3" v-model="createTaskParams.taskDescribe"> </el-input>
                  </el-form-item>
                  <el-form-item label="巡查事项：">
                    <!-- :on-preview="handlePreview"
                      :on-remove="handleRemove"
                      :before-remove="beforeRemove"
                      :on-exceed="handleExceed" -->
                    <el-upload
                      class="upload-demo"
                      :headers="headers"
                      :action="upaction + '/agenda/uploadFile'"
                      multiple
                      :limit="1"
                      :file-list="upFile"
                      :on-success="success"
                      :before-remove="beforeRemove"
                    >
                      <div style="display: flex">
                        <el-button size="small" type="primary">上传文件</el-button>
                        <el-select v-model="agendaId" placeholder="选择已上传文件" class="select-file">
                          <el-option
                            v-for="item in agendaFiles"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                          >
                          </el-option>
                        </el-select>
                      </div>
                      <div slot="tip" class="el-upload__tip" style="margin-left: 20px"></div>
                    </el-upload>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
          <span slot="footer">
            <!-- <el-button @click="resetFrom">重 置</el-button> -->
            <el-button type="primary" @click="subTaskPlan">确 定</el-button>
          </span>
        </el-dialog>
        <!-- 新建临时任务 -->
        <el-dialog :title="tempStatus ? '编辑临时任务' : '新建临时任务'" :visible.sync="createTempTaskWin" width="40%">
          <el-row>
            <el-col :span="20">
              <div class="grid-content bg-purple">
                <el-form label-position="right" label-width="120px" :model="createTaskParams">
                  <el-form-item label="任务类型：">
                    <el-select
                      v-model="createTaskParams.taskTypeId"
                      clearable
                      placeholder="请选择任务类型"
                      style="width: 100%"
                    >
                      <el-option v-for="item in typeName" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="巡查主体：">
                    <el-select
                      v-model="createTaskParams.parentId"
                      clearable
                      placeholder="请选择巡查主体"
                      style="width: 100%"
                    >
                      <el-option
                        v-for="item in patrolSubject"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="主体类型：">
                    <el-select
                      v-model="createTaskParams.principalTypeIds"
                      clearable
                      multiple
                      placeholder="请选择主体类型"
                      style="width: 100%"
                    >
                      <el-option v-for="item in subjectType" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="巡查范围：">
                    <el-cascader
                      clearable
                      ref="cascader"
                      v-model="createTaskParams.adcodes"
                      placeholder="请选择巡查范围"
                      :options="patrolGrid"
                      :props="{ multiple: true, emitPath: false, checkStrictly: true }"
                      :show-all-levels="false"
                      style="width: 100%"
                      @change="handleChange('cascader')"
                    >
                    </el-cascader>
                  </el-form-item>
                  <el-form-item label="开始时间：">
                    <div class="block">
                      <el-date-picker
                        v-model="createTaskParams.startTime"
                        type="date"
                        placeholder="选择开始日期："
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </div>
                  </el-form-item>
                  <el-form-item label="结束时间：">
                    <div class="block">
                      <el-date-picker
                        v-model="createTaskParams.endTime"
                        type="date"
                        placeholder="选择结束日期："
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </div>
                  </el-form-item>
                  <el-form-item label="任务描述：">
                    <el-input type="textarea" :rows="3" v-model="createTaskParams.taskDescribe"> </el-input>
                  </el-form-item>
                  <el-form-item label="巡查事项：">
                    <!-- :on-preview="handlePreview"
                      :on-remove="handleRemove"
                      :before-remove="beforeRemove"
                      :on-exceed="handleExceed" -->
                    <el-upload
                      class="upload-demo"
                      :headers="headers"
                      :action="upaction + '/agenda/uploadFile'"
                      multiple
                      :limit="1"
                      :file-list="upFile"
                      :on-success="success"
                      :before-remove="beforeRemove"
                    >
                      <div style="display: flex">
                        <el-button size="small" type="primary">上传文件</el-button>
                        <el-select v-model="agendaId" placeholder="选择已上传文件" class="select-file">
                          <el-option
                            v-for="item in agendaFiles"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                          >
                          </el-option>
                        </el-select>
                      </div>
                      <div slot="tip" class="el-upload__tip" style="margin-left: 20px"></div>
                    </el-upload>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
          <span slot="footer">
            <!-- <el-button @click="resetFrom">重 置</el-button> -->
            <el-button type="primary" @click="subTempTask">确 定</el-button>
          </span>
        </el-dialog>
        <!-- 任务进度 -->
        <el-dialog title="事项进度" :visible.sync="taskProgressWin" width="70%">
          <el-table :data="taskProgressData">
            <el-table-column prop="count" label="序号" width="50" align="center" type="index"></el-table-column>
            <el-table-column prop="enterpriseName" label="企业名称" width="200" align="center"></el-table-column>
            <el-table-column prop="addressEnterprise" label="企业地址" width="200" align="center"></el-table-column>
            <el-table-column prop="subjectName" label="主体" width="100" align="center"></el-table-column>
            <el-table-column prop="principalName" label="主体类型" width="150" align="center"></el-table-column>
            <el-table-column prop="fullNames" label="负责人" width="100" align="center"></el-table-column>
            <el-table-column prop="schedule" label="事项进度" width="100" align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="200" align="center"></el-table-column>
            <el-table-column fixed="right" width="200" label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="info" @click="openCompanyDetailWin(scope.row)" size="mini">详情</el-button>
                <!-- 巡查事项与巡查事项进度用的是一个抽屉，数据不同 -->
                <el-button type="warning" @click="checkFile(scope.row)" size="mini">巡查事项</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
        <!-- 事项进度的公司详情 -->
        <el-dialog title="详情" :visible.sync="companyDetailWin" width="30%">
          <el-row>
            <el-col :span="20">
              <div class="grid-content bg-purple">
                <el-form label-position="right" label-width="120px" :model="companyDetailData">
                  <el-form-item label="企业名称：">
                    <el-input v-model="companyDetailData.enterpriseName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="企业地址：">
                    <el-input v-model="companyDetailData.addressEnterprise" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="主体：">
                    <el-input v-model="companyDetailData.principalName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="主体类型：">
                    <el-input v-model="companyDetailData.principalName" :disabled="true"></el-input>
                  </el-form-item>
                  <el-form-item label="法定代表人：">
                    <el-input v-model="companyDetailData.superintendent" :disabled="true">法人未知</el-input>
                  </el-form-item>
                  <el-form-item label="联系电话：">
                    <el-input v-model="companyDetailData.superintendentPhone" :disabled="true">电话未知</el-input>
                  </el-form-item>
                  <el-form-item label="事项进度：">
                    <el-input v-model="companyDetailData.schedule" :disabled="true"></el-input>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as api from '@/api/project'
import env from '@/utils/env'
export default {
  data() {
    return {
      // 表格数据源
      tableData: [],
      // 处置弹窗
      distributionWin: false,
      // 新建弹窗
      createTaskWin: false,
      // 新建临时任务弹窗
      createTempTaskWin: false,
      // 详情弹窗
      taskDetailWin: false,
      // 任务进度弹窗
      taskProgressWin: false,
      // 公司详情弹窗
      companyDetailWin: false,
      // 巡查事项抽屉
      patrolFileWin: false,
      // 新建任务数据源，后续赋值
      createTaskData: {},
      // 任务类型
      typeName: [],
      // 巡查主体
      patrolSubject: [],
      // 主体类型
      subjectType: [],
      // 巡查范围
      patrolGrid: [],
      // 计划列表，新建任务需根据计划来建立任务
      planList: [],
      // 上传文件，新建时传入后端的文件地址
      upFile: [],
      // 文件地址
      upaction:
        process.env.NODE_ENV === 'production'
          ? env.prod.baseUrl
          : process.env.NODE_ENV === 'test'
          ? env.test.baseUrl
          : env.dev.baseUrl,
      // 设置上传文件的请求头
      headers: { Authorization: '' },
      // 所有已上传事项文件
      agendaFiles: [],
      // 新建入参
      agendaId: '',
      // 新建任务入参
      createTaskParams: {
        adcodes: [],
        agendaId: 0,
        endTime: '',
        parentId: '',
        principalTypeIds: [],
        specialPlanId: '',
        startTime: '',
        taskDescribe: '',
        taskTypeId: '',
      },
      // 任务进度数据
      taskProgressData: [],
      // 公司详情数据
      companyDetailData: {},
      // 任务详情数据
      taskDetailData: {},
      // 从详情查看的事项文件
      patrolFile: [],
      // 任务编辑数据----------------------
      // 任务处置数据
      distributionData: {},
      // 岗位处置角色
      roleList: [],
      // 处置入参
      distributionParams: {
        id: '',
        roleId: '',
      },
      // 批量删除入参
      deleteTasksParams: [],
      // 多选
      multipleSelection: [],
      // 状态字典
      taskStatus: ['', '未处置', '已处置'],
      // 进度字典
      taskProgress: ['未创建', '未开始', '进行中', '已完成', '已超时'],
      // 新建和编辑
      changeStatus: 0,
      tempStatus: 0,
      // 表头
      tableHeader: [],
      // 表单主体
      tableBody: [],
    }
  },
  created() {
    this.taskInfo()
  },
  mounted() {
    this.token = localStorage.getItem('Authorization')
    this.headers = { Authorization: this.token }
  },
  methods: {
    // 分页多条件查询
    taskInfo() {
      let data = {
        current: 1,
        limit: 10,
      }
      api.taskInfo(data).then(res => {
        if (res.code == '200') {
          console.log('表格源数据', res)
          let count = 1
          this.tableData = res.data.records
          this.tableData.forEach(item => {
            item.startTime = item.startTime.split(' ')[0]
            item.endTime = item.endTime.split(' ')[0]
            item.patrolGrid = Object.values(item.patrolGrid).join('，')
            item.subjectType = Object.values(item.subjectType).join('，')
            item.taskStatus = this.taskStatus[item.taskStatus]
            item.taskProgress = this.taskProgress[item.taskProgress]
            item.count = count++
          })
        }
      })
    },
    // 新建任务
    addTask() {
      this.createTaskWin = true
      this.changeStatus = 0
      this.agendaId = ''
      this.planList = []
      this.createTaskData = {}
      this.createTaskParams = {
        adcodes: [],
        agendaId: 0,
        endTime: '',
        parentId: '',
        principalTypeIds: [],
        specialPlanId: '',
        startTime: '',
        taskDescribe: '',
        taskTypeId: '',
      }
      this.getFiles()
      api.getPlanList({}).then(res => {
        if (res.code == '200') {
          this.planList = res.data.map(item => {
            return { value: item.id, label: item.planName }
          })
        }
      })
    },
    // 新建临时任务
    tempTask() {
      this.createTempTaskWin = true
      this.tempStatus = 0
      ;(this.createTaskParams = {
        adcodes: [],
        agendaId: '',
        endTime: '',
        parentId: '',
        principalTypeIds: [],
        startTime: '',
        taskDescribe: '',
        taskTypeId: '',
      }),
        this.getTaskType()
      this.getSubject()
      this.getAddress()
      this.getFiles()
    },
    // 编辑任务
    openEditWin(row) {
      if (row.planNumber) {
        console.log('有任务计划编号', row.planNumber)
        this.createTaskWin = true
        this.changeStatus = 1
        this.planList = []
        this.getFiles()
        api.getPlanList({}).then(res => {
          if (res.code == '200') {
            this.planList = res.data.map(item => {
              return { value: item.id, label: item.planName }
            })
            this.createTaskParams.specialPlanId = row.specialPlanId
          }
        })

        this.createTaskParams.id = row.id
        this.agendaId = row.agendaId
        this.createTaskParams.startTime = row.startTime + ' 00:00:00'
        this.createTaskParams.endTime = row.endTime + ' 00:00:00'
        this.createTaskParams.taskDescribe = row.taskDescribe
        console.log('计划编号二', this.createTaskParams.specialPlanId)
      } else {
        // 编辑临时任务
        this.createTempTaskWin = true
        this.tempStatus = 1
        // this.createTaskParams = {
        //   adcodes: [],
        //   agendaId: 0,
        //   endTime: '',
        //   parentId: '',
        //   principalTypeIds: [],
        //   specialPlanId: '',
        //   startTime: '',
        //   taskDescribe: '',
        //   taskTypeId: '',
        // }
        this.getTaskType()
        this.getSubject()
        this.getAddress()
        this.getSubjectType(row.parentId)
        this.getFiles()
        api.getTaskById({ id: row.id }).then(res => {
          if (res.code == '200') {
            this.createTaskParams.taskTypeId = res.data.taskTypeId
            this.createTaskParams.parentId = res.data.parentId
            this.createTaskParams.principalTypeIds = Object.keys(res.data.subjectType).map(item => parseInt(item))
            this.createTaskParams.adcodes = Object.keys(res.data.patrolGrid)
            this.createTaskParams.startTime = res.data.startTime
            this.createTaskParams.endTime = res.data.endTime
            this.createTaskParams.taskDescribe = res.data.taskDescribe
            this.agendaId = res.data.agendaId
            this.createTaskParams.id = res.data.id
          }
        })
      }
    },
    // 新建任务--->所属计划--->计划详情--->入参赋值，另一个变量回显
    getPlan(val) {
      api.taskDetail({ id: val }).then(res => {
        if (res.code == '200') {
          this.createTaskData = res.data
          // 入参
          this.createTaskParams.adcodes = Object.keys(res.data.patrolGrid)
          this.createTaskParams.principalTypeIds = Object.keys(res.data.subjectType)
          this.createTaskParams.parentId = res.data.parentId
          this.createTaskParams.taskTypeId = res.data.taskTypeId
          // 回显数据
          this.createTaskData.subjectType = Object.values(res.data.subjectType).join('，')
          this.createTaskData.patrolGrid = Object.values(res.data.patrolGrid).join('，')
          this.createTaskData.typeName = res.data.typeName
        }
      })
    },
    // 上传文件获取url
    success(res) {
      if (res.code == '200') {
        this.agendaId = ''
        this.createTaskParams.agendaId = res.data.id
        this.$message({
          message: '上传成功!',
          type: 'success',
        })
      }
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    // 提交新建或修改任务
    subTaskPlan() {
      if (this.changeStatus) {
        api.editTask(this.createTaskParams).then(res => {
          if (res.code == '200') {
            this.$message({
              message: '修改成功!',
              type: 'success',
            })
            this.createTaskWin = false
            this.taskInfo()
          } else {
            this.$message({
              message: '修改失败!',
              type: 'warning',
            })
          }
        })
      } else {
        api.addTask(this.createTaskParams).then(res => {
          if (res.code == '200') {
            this.$message({
              message: '任务创建成功!',
              type: 'success',
            })
            this.createTaskWin = false
            this.taskInfo()
          } else {
            this.$message({
              message: '创建失败!',
              type: 'warning',
            })
          }
        })
      }
    },
    // 提交新建或修改临时任务
    subTempTask() {
      if (this.tempStatus) {
        console.log('编辑临时任务入参', this.createTaskParams)
        api.editTask(this.createTaskParams).then(res => {
          if (res.code == '200') {
            this.$message({
              message: '修改成功!',
              type: 'success',
            })
            this.createTempTaskWin = false
            this.taskInfo()
          } else {
            this.$message({
              message: '修改失败!',
              type: 'warning',
            })
          }
        })
      } else {
        api.addTask(this.createTaskParams).then(res => {
          if (res.code == '200') {
            this.$message({
              message: '临时任务创建成功!',
              type: 'success',
            })
            this.createTempTaskWin = false
            this.taskInfo()
          } else {
            this.$message({
              message: '创建失败!',
              type: 'warning',
            })
          }
        })
      }
    },
    // 单个删除
    deleteTask(row) {
      this.$confirm('是否删除此任务?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          api.deleteTask({ id: row.id }).then(res => {
            if (res.code == '200') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.taskInfo()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          })
        })
    },
    // 批量删除任务
    deleteTasks() {
      if (!this.deleteTasksParams.length) {
        return
      }
      this.$confirm('是否批量删除任务?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          let data = 'ids=' + this.deleteTasksParams.join(',')
          api.deleteTasks(data).then(res => {
            if (res.code == '200') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.taskInfo()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          })
        })
    },
    // 批量选中
    handleSelectionChange(val) {
      this.deleteTasksParams = val.map(item => item.id)
    },
    // 处置任务弹窗
    openDistributionWin(row) {
      this.distributionWin = true
      this.distributionData = row
      this.roleList = []
      api.roleList({}).then(res => {
        if (res.code == '200') {
          res.data.records.forEach(item => {
            this.roleList.push({ value: item.id, label: item.roleName })
          })
        }
      })
    },
    // 任务处置
    distributionTask() {
      this.distributionWin = false
      this.distributionParams.id = this.distributionData.id
      api.distributionTask(this.distributionParams).then(res => {
        if (res.code == '200') {
          this.$message({
            type: 'success',
            message: '任务处置成功!',
          })
        } else {
          this.$message({
            type: 'warning',
            message: '任务处置失败!',
          })
        }
      })
    },
    // 任务详情
    openDetailWin(row) {
      this.taskDetailWin = true
      this.taskDetailData = row
    },
    // 巡查事项文件或者一个公司的巡查事项进度的-----抽屉
    checkFile(val) {
      this.patrolFileWin = true
      if (typeof val == 'string') {
        console.log('巡查事项文件', JSON.parse(val))
        let patrolFile = JSON.parse(val)
        // 处理表头
        this.tableHeader = patrolFile[0]
        // 处理表主体
        this.tableBody = []
        patrolFile.forEach((item, index) => {
          if (index > 0) {
            this.tableBody.push(item)
          }
        })
      } else {
        api.getCompanyDetail({ id: val.id }).then(res => {
          if (res.code == '200') {
            console.log('巡查文件数据', JSON.parse(res.data.mattersFile))
            let patrolFile = JSON.parse(res.data.mattersFile)
            // 处理表头
            this.tableHeader = patrolFile[0]
            // 处理表主体
            this.tableBody = []
            patrolFile.forEach((item, index) => {
              if (index > 0) {
                this.tableBody.push(item)
              }
            })
          }
        })
      }
    },
    // 任务事项弹窗
    openProgressWin(row) {
      this.taskProgressWin = true
      // 请求巡查范围内的所有企业信息
      api.getTaskProceed({ current: 1, limit: 10, taskInfoId: row.id }).then(res => {
        if (res.code == '200') {
          this.taskProgressData = res.data.records
          this.taskProgressData.forEach(item => {
            if (item.fullNames) {
              item.fullNames = item.fullNames.join('，')
            }
            item.schedule = this.taskProgress[item.schedule]
          })
        }
      })
    },
    // 公司详情弹窗
    openCompanyDetailWin(row) {
      this.companyDetailWin = true
      api.getCompanyDetail({ id: row.id }).then(res => {
        if (res.code == '200') {
          this.companyDetailData = res.data
          this.companyDetailData.schedule = this.taskProgress[this.companyDetailData.schedule]
        }
      })
    },
    // 获取巡查范围
    getAddress() {
      this.patrolGrid = []
      api.getAddress({ pid: 441300 }).then(res => {
        if (res.code == '200') {
          res.data.forEach((item, index) => {
            this.patrolGrid.push({ value: item.zoningCode, label: item.name, children: [] })
            api.getAddress({ pid: item.id }).then(res => {
              if (res.code == '200' && res.data.length > 0) {
                res.data.forEach((cell, index2) => {
                  this.patrolGrid[index].children.push({ value: cell.zoningCode, label: cell.name, children: [] })
                  api.getAddress({ pid: cell.id }).then(res => {
                    if (res.code == '200' && res.data.length > 0) {
                      res.data.forEach(roll => {
                        this.patrolGrid[index].children[index2].children.push({
                          value: roll.zoningCode,
                          label: roll.name,
                        })
                      })
                    } else {
                      this.patrolGrid[index].children[index2].children = null
                    }
                  })
                })
              } else {
                this.patrolGrid[index].children = null
              }
            })
          })
        }
      })
    },
    // 选中巡查范围处理
    handleChange(value) {
      console.log('巡查的value', value)
      let checkedNodeList = this.$refs[value].getCheckedNodes()
      console.log('巡查范围', checkedNodeList)
      checkedNodeList = checkedNodeList.filter(item => {
        if (item.level == 1) {
          return item
        } else if (item.level == 2) {
          if (!item.parent.checked) {
            return item
          }
        } else if (item.level == 3) {
          if (!item.parent.parent.checked && !item.parent.checked) {
            return item
          }
        }
      })
      this.createTaskParams.adcodes = checkedNodeList.map(item => item.value)
    },
    // 获取任务类型
    getTaskType() {
      api.taskType().then(res => {
        this.typeName = []
        if (res.code == '200') {
          res.data.forEach(item => {
            this.typeName.push({ value: item.id, label: item.typeName })
          })
        }
      })
    },
    // 获取巡查主体
    getSubject() {
      let data = {
        parentId: 0,
      }
      api.getChildSubject(data).then(res => {
        this.patrolSubject = []
        if (res.code == '200') {
          res.data.forEach(item => {
            this.patrolSubject.push({ value: item.id, label: item.principalName })
          })
        }
      })
    },
    // 获取主体类型
    getSubjectType(id) {
      let data = {
        parentId: id,
      }
      api.getChildSubject(data).then(res => {
        this.subjectType = []
        if (res.code == '200') {
          if (!this.tempStatus) {
            this.createTaskParams.principalTypeIds = []
          }
          res.data.forEach(item => {
            this.subjectType.push({ value: item.id, label: item.principalName })
          })
        }
      })
    },
    // 获得已上传的文件
    getFiles() {
      this.agendaId = ''
      api.getAgendaFiles({}).then(res => {
        if (res.code == '200') {
          this.agendaFiles = res.data
            .filter(item => item != null)
            .map(cell => {
              return { value: cell.id, label: cell.fileName }
            })
        }
      })
    },
  },
  watch: {
    // 侦听计划的id
    'createTaskParams.specialPlanId'(newVal) {
      if (!newVal) {
        return
      }
      this.getPlan(newVal)
    },
    // 侦听上传文件的id
    agendaId(newVal) {
      this.createTaskParams.agendaId = newVal
    },
    // 侦听巡查主体的变化
    'createTaskParams.parentId'(newVal) {
      if (!newVal) {
        return
      }
      this.getSubjectType(newVal)
    },
  },
}
</script>

<style lang="scss" scoped>
.select {
  background-color: pink;
  margin-bottom: 30px;
}
.handle-task {
  display: flex;
}
.add-task {
  margin: 0 20px 10px 0;
}
.show-plan {
  margin-bottom: 20px;
}
.select-file {
  width: 150px;
  margin-left: 20px;
}
.patrol-file {
  width: 98%;
  border-collapse: collapse;
  margin: auto;
}
</style>
